﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

using Willson.JsbManager.Idal;
using Willson.JsbManager.Entity;
using Willson.JsbManager.BllEntity;
using Willson.JsbManager.Comm;

namespace Willson.JsbManager.Bll
{
    public class BLLJhManager:BaseBLL
    {
        private ITableXtCompany xt_company_DAL = null;
        private ITableXtSbJd xt_sb_jd_DAL = null;
        private ITableXtSbMonth xt_sb_month_DAL = null;
        private ITableXtCompany_sb_info xt_company_sb_info_DAL = null;
        
        public BLLJhManager()
        {
            xt_company_DAL = Reflect<ITableXtCompany>.Create("DAL_table_xt_company", "Willson.JsbManager.Dal");
            xt_sb_jd_DAL = Reflect<ITableXtSbJd>.Create("DAL_table_xt_sb_jd", "Willson.JsbManager.Dal");
            xt_sb_month_DAL = Reflect<ITableXtSbMonth>.Create("DAL_table_xt_sb_month", "Willson.JsbManager.Dal");
            xt_company_sb_info_DAL = Reflect<ITableXtCompany_sb_info>.Create("DAL_table_xt_company_sb_info", "Willson.JsbManager.Dal");
        }

        public void CreateCjh(int year, int jd)
        {
            Hashtable hash = new Hashtable();

            hash.Add("remove", 0);

            int li_cjh = 0,li_jh = 0;

            IEnumerator<TableXtCompanyEntity> ator = xt_company_DAL.Find(hash, SqlOperator.And, true).GetEnumerator();

            hash.Clear();

            while (ator.MoveNext())
            {
                hash.Add("company_id", ator.Current.Id);
                hash.Add("sb_year", year);
                hash.Add("sb_jd", jd);

                TableXtSbJdEntity jd_entity = xt_sb_jd_DAL.Find(hash);

                hash.Clear();

                if (jd_entity == null)
                {
                    continue;
                }

                if (jd_entity.Jd_jh_begin > 0)
                {
                    li_jh = jd_entity.Jd_jh_begin;
                }
                else
                {
                    li_jh = jd_entity.Jd_jh;
                }

                if (jd_entity.Jd_sum > li_jh)
                {
                    li_cjh = jd_entity.Jd_sum - li_jh;

                    hash.Add("jd_cjh",li_cjh);
                    hash.Add("jd_scjh",li_cjh);

                    xt_sb_jd_DAL.Update(jd_entity.Id, hash);

                    hash.Clear();

                }

            }
        }

        public void CreateJdSum(int year, int jd)
        {
            int li_ysl_one = 0, li_ysl_two = 0, li_ysl_three = 0;
            int li_sum = 0;

            Hashtable hash = new Hashtable();
            Hashtable update = new Hashtable();

            hash.Add("remove", 0);

            IEnumerator<TableXtCompanyEntity> ator = xt_company_DAL.Find(hash, SqlOperator.And, true).GetEnumerator();

            hash.Clear();

            while (ator.MoveNext())
            {
                hash.Add("company_id", ator.Current.Id);
                hash.Add("remove", 0);

                IEnumerator<TableXtCompanySbInfoEntity> ator1 = xt_company_sb_info_DAL.Find(hash, SqlOperator.And, true).GetEnumerator();

                hash.Clear();

                li_sum = 0;

                while (ator1.MoveNext())
                {

                    if (jd == 1)
                    {
                        li_ysl_one = GetMonthYsl(ator1.Current.Id,year, 1);
                        li_ysl_two = GetMonthYsl(ator1.Current.Id, year, 2);
                        li_ysl_three = GetMonthYsl(ator1.Current.Id, year, 3);
                    }

                    if (jd == 2)
                    {
                        li_ysl_one = GetMonthYsl(ator1.Current.Id, year, 4);
                        li_ysl_two = GetMonthYsl(ator1.Current.Id, year, 5);
                        li_ysl_three = GetMonthYsl(ator1.Current.Id, year, 6);
                    }

                    if (jd == 3)
                    {
                        li_ysl_one = GetMonthYsl(ator1.Current.Id, year, 7);
                        li_ysl_two = GetMonthYsl(ator1.Current.Id, year, 8);
                        li_ysl_three = GetMonthYsl(ator1.Current.Id, year, 9);
                    }

                    if (jd == 4)
                    {
                        li_ysl_one = GetMonthYsl(ator1.Current.Id, year, 10);
                        li_ysl_two = GetMonthYsl(ator1.Current.Id, year, 11);
                        li_ysl_three = GetMonthYsl(ator1.Current.Id, year, 12);
                    }

                    li_sum += li_ysl_one + li_ysl_two + li_ysl_three;

                }

                hash.Add("company_id", ator.Current.Id);
                hash.Add("sb_year", year);
                hash.Add("sb_jd", jd);

                update.Add("jd_sum",li_sum);

                xt_sb_jd_DAL.Update(update, hash);

                hash.Clear();
                update.Clear();

            }
           
        }

        private int GetMonthYsl(int sb_id,int year,int month)
        {
            Hashtable hash = new Hashtable();
            Table_xt_sb_month_entity entity = null;

            hash.Add("sb_id", sb_id);
            hash.Add("sb_year", year);
            hash.Add("sb_month", month);
  
            entity = xt_sb_month_DAL.Find(hash);

            return entity.Sb_ysl;
        }

        public void CreateJdJh(int year,int jd,ref List<JdJhError> info)
        {
            Hashtable hash = new Hashtable();
   
            hash.Add("remove", 0);

            IEnumerator<TableXtCompanyEntity> ator = xt_company_DAL.Find(hash, SqlOperator.And, true).GetEnumerator();

            hash.Clear();

            while (ator.MoveNext())
            {
                hash.Add("company_id", ator.Current.Id);
                hash.Add("sb_year", year - 1);
                hash.Add("sb_jd", jd);

                TableXtSbJdEntity jd_entity = xt_sb_jd_DAL.Find(hash);

                hash.Clear();

                if (jd_entity == null)
                {
                    JdJhError error = new JdJhError();
                    error.CompanyName = ator.Current.Name;
                    error.CompanyAddress = ator.Current.Address;
                    error.CompanyID = ator.Current.Id;

                    info.Add(error);

                    continue;
                }

                bool findIt = false;
                
                hash.Add("company_id", ator.Current.Id);
                hash.Add("sb_year", year);
                hash.Add("sb_jd", jd);

                TableXtSbJdEntity jd_entity_test = xt_sb_jd_DAL.Find(hash);

                hash.Clear();

                if (jd_entity_test != null) findIt = true;

                int Jd_jh = 0;

                if (jd_entity.Jd_sum < 1200)
                    Jd_jh = 1200;
                else
                    Jd_jh = jd_entity.Jd_sum;

                //if (jd_entity.Jd_jh_begin == 0)
                //{
                //    if (jd_entity.Jd_jh_add == 0)
                //        Jd_jh = jd_entity.Jd_jh;
                //    else
                //        Jd_jh = jd_entity.Jd_jh + jd_entity.Jd_jh_add;
                //}
                //else
                //{
                //    if (jd_entity.Jd_jh_add == 0)
                //        Jd_jh = jd_entity.Jd_jh_begin;
                //    else
                //        Jd_jh = jd_entity.Jd_jh_begin + jd_entity.Jd_jh_add;
                //}

                if (findIt)
                {
                    hash.Add("jd_jh", Jd_jh);

                    xt_sb_jd_DAL.Update(jd_entity_test.Id, hash);

                    hash.Clear();
                }
                else
                {
                    TableXtSbJdEntity entity = new TableXtSbJdEntity();

                    entity.Company_id = ator.Current.Id;
                    entity.Sb_year = year;
                    entity.Sb_jd = jd;
                    entity.Jd_jh = Jd_jh;
                    
                    xt_sb_jd_DAL.Insert(entity);
                }

            }

        }
    }
}
